$(function(){
	// 页面加载完成建立分页
	showPage(current,pages);

	// 实现方法
	list = function(page){
		var ll = layer.load();
		
		if(!page){
			page = 1;
		}
		
		searchForm.pageNum.value = page;
		
		$.post(basePath + "/voltageLevel/list",$("#searchForm").serialize(),function(data){
			$("#contentTBody").html(data);
			showPage(current,pages);
			layer.close(ll);
		},'html');
	};
	
	add = function(){
		var ll = layer.load();
		$.post(basePath + "/voltageLevel/detail",{},function(data){
			layer.open({
			  type: 1,
			  title: '新增',
			  closeBtn: 1,
			  area: ['600px', '200px'],
			  shadeClose: true,
			  content: data,
			  success: function(layero, index){
			    $(layero).find(".controls input,.controls textarea").each(function(index,element){
			    	var handleMsg = $(element).parents(".control-group").find(".control-label").eq(0).text();
			    	var errorMsgElement = $(element).next(".help-inline").eq(0);
			    	
	    			for(var f in checkFuntion){
	    				if($(element).hasClass(f)){
					    	for(var e in bindEvent){
					    		$(element).bind(bindEvent[e],{f:checkFuntion[f],element:element,msg:handleMsg,errorMsgElement:errorMsgElement},function(event){
					    			event.data.f(event.data.element,event.data.msg,event.data.errorMsgElement);
					    		});
					    	}
	    				}
	    			}
			    });
			  },
			  btn: ['确认', '取消'],
			  yes: function(index, layero){
				  var sl = layer.load();
				  save(null,sl,index);
			  }
			});
			
			layer.close(ll);
		},'html');
	};
	
	modify = function(seqId){
		var ll = layer.load();
		$.post(basePath + "/voltageLevel/detail",{seqId: seqId},function(data){
			layer.open({
			  type: 1,
			  title: '修改',
			  closeBtn: 1,
			  area: ['600px', '200px'],
			  shadeClose: true,
			  content: data,
			  success: function(layero, index){
			    $(layero).find(".controls input,.controls textarea").each(function(index,element){
			    	var handleMsg = $(element).parents(".control-group").find(".control-label").eq(0).text();
			    	var errorMsgElement = $(element).next(".help-inline").eq(0);
			    	
	    			for(var f in checkFuntion){
	    				if($(element).hasClass(f)){
					    	for(var e in bindEvent){
					    		$(element).bind(bindEvent[e],{f:checkFuntion[f],element:element,msg:handleMsg,errorMsgElement:errorMsgElement},function(event){
					    			event.data.f(event.data.element,event.data.msg,event.data.errorMsgElement);
					    		});
					    	}
	    				}
	    			}
			    });
			  },
			  btn: ['确认', '取消'],
			  yes: function(index, layero){
				  var sl = layer.load();
				  save(seqId,sl,index);
			  }
			});
			
			layer.close(ll);
		},'html');
	};
	
	save = function(seqId,sl,lidx){
		$("#dataForm").find(".controls input,.controls textarea").blur();
		
		if($("#dataForm").find(".check-error").length > 0){
			layer.close(sl);
			return false;
		}
		
		if(!!seqId){
			$.post(basePath + "/voltageLevel/update",$("#dataForm").serialize(),function(data){
				layer.close(sl);
				var result = dealData(data);
				if(result){
					layer.close(lidx);
					list();
				}
			},'html');
		}
		else{
			$.post(basePath + "/voltageLevel/add",$("#dataForm").serialize(),function(data){
				layer.close(sl);
				var result = dealData(data);
				if(result){
					layer.close(lidx);
					list();
				}
			},'html');
		}
	};
	
	del = function(seqId){
		var ll = layer.load();
			
		$.post(basePath + "/voltageLevel/delete",{seqId : seqId},function(data){
			layer.close(ll);
			
			switch(data){
			case 'RR_ES' :
				var l1 = layer.open({
				  type: 1,
				  title: '删除该电压等级',
				  closeBtn: 1,
				  area: ['300px', '200px'],
				  shadeClose: true,
				  content: '<div class="alert alert-error" style="margin: 0">请确认是否<strong>删除该电压等级</strong>，' + 
				  			'删除电压等级后<strong>对应的故障记录也会被删除</strong><br/>' + 
				  			'您也可以选择先去将该电压等级的数据<strong>移动到另一电压等级下</strong>。</div>',
				  btn: ['确认删除', '先去移动'],
				  yes: function(index, layero){
					  $.post(basePath + "/voltageLevel/cleanData",{seqId : seqId},function(data){
						  var result = dealData(data);
						  if(result){
							layer.close(l1);
							list();
						  }
					  });
				  },
				  btn2: function(index, layero){
					layer.close(l1);
					move(seqId);
				  }
				});
				break;
			case 'DB_ER' :
				layer.msg('删除时遇到错误', {
					time: 2000, //2s后自动关闭
					btn: ['关闭'],
					icon: 2,
					title: '消息提示'
				});
				break;
			case 'success' :
				layer.msg('删除成功', {
					time: 1000, //1s后自动关闭
					icon: 1,
					title: '消息提示'
				});
				list();
				break;
			}
			
		},'html');
	};
	
	move = function(seqId){
		var ll = layer.load();
		
		$.post(basePath + "/voltageLevel/moveList",{oldSeqId: seqId},function(data){
			moveLayerId = layer.open({
			  type: 1,
			  title: '移动数据',
			  closeBtn: 1,
			  area: ['600px', '600px'],
			  shadeClose: true,
			  content: data
			});
			
			layer.close(ll);
		},'html');
	};
	
	moveList = function(pageNum,seqId){
		var ll = layer.load();
		moveSearchForm.pageNum.value = pageNum;
		
		$.post(basePath + "/voltageLevel/moveList",$("#moveSearchForm").serialize(),function(data){
			$("#moveSearchForm").parent().html(data);
			layer.close(ll);
		},'html');
	};
	
	moveData = function(oldSeqId,newSeqId){
		var ll = layer.load();
		
		$.post(basePath + "/voltageLevel/moveData",{seqId : oldSeqId,newSeqId : newSeqId},function(data){
			layer.close(ll);
			
			var result = dealData(data);
			if(result){
				layer.close(moveLayerId);
				list();
			}
		},'html');
	};
	
	var dealData = function(data){
		var msg = '操作失败';
		switch(data){
		case 'VV_NP' : msg = '电压等级不能为空';break;
		case 'ID_NP' : console.error('更新时传入后台的更新依据为空');break;
		case 'DB_ER' : console.error('执行数据库操作时发生异常');break;
		case 'success' : msg = '操作成功';break;
		}
		
		if(data == 'success'){
			layer.msg(msg, {
				time: 1000, //1s后自动关闭
				icon: 1,
				title: '消息提示'
			});
			return true;
		}
		else{
			layer.msg(msg, {
				time: 2000, //2s后自动关闭
				btn: ['关闭'],
				icon: 2,
				title: '消息提示'
			});
			return false;
		}
	}
});